﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Util;

namespace BibleBase
{
    [Serializable]
    public class Bible
    {
        public List<Book> Books { get; set; }

        public Bible()
        {
            Books = new List<Book>();
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("Biblia version: {0}", Name));
            sb.AppendLine();
            Books.ForEach(x => sb.AppendLine(x.ToString()));
            return sb.ToString();
        }

        public string Name { get; set; }

        public string ConvertToCompatibleBook(string bookName)
        {
            string[] parts = bookName.Split(new char[] { ' ', ' ' });
            if (parts.Length > 1)
            {
                int num;
                if (!int.TryParse(parts[0], out num))
                    throw new Exception("Must be a number!");
                return string.Format("{0}, {1}", parts[0], parts[1]);
            }
            return bookName;
        }

        public string GetPlainPassage(string bookName, int chapterNumber, int verseNumber)
        {
            bookName = ConvertToCompatibleBook(bookName);
            Book book = this.Books.First(x => x.Name == bookName);
            Chapter chapter = book.Chapters.First(x => x.Number == chapterNumber);
            Verse verse = chapter.Verses.First(x => x.Number == verseNumber);
            return verse.Text;
        }

        public static bool CompareBibles(Bible bible1, Bible bible2)
        {
            if (bible1.Books.Count != bible2.Books.Count)
                return false;
            for (int i = 0; i < bible1.Books.Count; i++)
            {
                if (!CompareBooks(bible1.Books[i], bible2.Books[i]))
                {
                    bible1.Books.RemoveAt(i);
                    bible2.Books.RemoveAt(i);
                }
            }
            return true;
        }

        public static bool CompareBooks(Book book1, Book book2)
        {
            if (book1.Chapters.Count != book2.Chapters.Count)
                return false;
            for (int i = 0; i < book1.Chapters.Count; i++)
            {
                if (!CompareChapters(book1.Chapters[i], book2.Chapters[i]))
                {
                    book1.Chapters.RemoveAt(i);
                    book2.Chapters.RemoveAt(i);
                }
            }
            return true;
        }

        public static bool CompareChapters(Chapter chapter1, Chapter chapter2)
        {
            if (chapter1.Verses.Count != chapter2.Verses.Count)
                return false;
            return true;
        }

        public void Save(string savePath)
        {
            PersistentObject.Serialize<Bible>(savePath, this);
        }
    }
}
